home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / text / misc / thai-conv.lha / thaiconv.cc < prev    next >
C/C++ Source or Header  |  2002-09-24  |  3KB  |  116 lines

  1. /**
  2.  * @file  thaiconv.cc
  3.  * @brief Convert from UTF-8 to TIS-620 coded text
  4.  * author Lyndon Hill (koan) 
  5.  * last edit 11.09.02 Catching invalid UTF8
  6.  *           03.09.02 Added reverse direction for coding
  7.  *           09.08.02 First working version
  8.  * note: build with > g++ thaiconv.cc -o thaiconv -lm
  9.  *
  10.  * (c) 2002. Permission given to aminet for distribution
  11.  */
  12.  
  13. #include <iostream>
  14. #include <fstream>
  15. #include <math.h>
  16.  
  17. using namespace std;
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.   int a, bitval;
  22.  
  23.   if((argc < 2) || (argc > 3))
  24.   {
  25.     cout << "Usage: thaiconv in-file      [convert file to TIS-620]\n";
  26.     cout << "       thaiconv in-file utf8 [convert file to UTF-8]\n";
  27.     exit(0);
  28.   }
  29.  
  30.   char *infile = argv[1];
  31.   bool tis620 = true;
  32.   if(argc == 3) tis620 = false;
  33.  
  34.   // open file
  35.  
  36.   ifstream myinput(infile);
  37.   if(myinput.bad())
  38.   {
  39.     cout << "Could not open '" << infile << "'\n";
  40.     exit(0);
  41.   }
  42.  
  43.   // read data
  44.  
  45.   unsigned char temp;
  46.   unsigned char temp2;
  47.   unsigned char temp3;
  48.   int total;
  49.  
  50.   // stop at end of file
  51.   while(temp = myinput.get())
  52.   {
  53.     if(myinput.eof())
  54.       break;
  55.  
  56.     // convert coding
  57.     if((int)(temp) > 127)
  58.     {
  59.       if(tis620 == true)
  60.       {
  61.         if((temp >= 0xc0) && (temp <= 0xfd))
  62.         {
  63.           // valid utf 8
  64.           // deal with multibyte coding
  65.           temp2 = myinput.get();        // second byte, should check for error
  66.           if((temp != 224) && ((temp2 < 184) || (temp2 > 187))) break; // not Thai 
  67.           temp3 = myinput.get();        // here too
  68.           temp -= 224; temp *= 16384;
  69.           temp2 -= 128; temp2 *= 64;
  70.           temp3 -= 128;
  71.           total = temp+temp2+temp3;
  72.  
  73.           total = total - 0xe00 + 0xa0; // convert UTF -> TIS
  74.           cout << (unsigned char)(total); // output ascii equivalent 
  75.         }
  76.       }
  77.       else
  78.       {
  79.         // convert to multibyte coding
  80.         total = (int)(temp) - 0xa0 + 0xe00; // convert TIS -> UTF
  81.         temp = 224; temp2 = 128; temp3 = 128;
  82.         for(a = 15; a >= 12; a--)
  83.         {
  84.           bitval = pow(2.0, a);
  85.           if(total >= bitval)
  86.           {
  87.             total -= bitval;
  88.             temp += pow(2.0, a-12);
  89.           }
  90.         }
  91.         for(a = 11; a >= 6; a--)
  92.         {
  93.           bitval = pow(2.0, a);
  94.           if(total >= bitval)
  95.           {
  96.             total -= bitval;
  97.             temp2 += pow(2.0, a-6);
  98.           }
  99.         }
  100.         for(a = 5; a >= 0; a--)
  101.         {
  102.           bitval = pow(2.0, a);
  103.           if(total >= bitval)
  104.           {
  105.             total -= bitval;
  106.             temp3 += pow(2.0, a);
  107.           }
  108.         }
  109.         cout << temp << temp2 << temp3;
  110.       }
  111.     }
  112.     else cout << temp; // standard 7 bit ascii
  113.   }
  114.  
  115.   exit(1);
  116. }